//
//  SpecialViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "SpecialViewController.h"
#import "SpecialDetailViewController.h"
#import "NetWorkingManager.h"
#import "ZJRefurbish.h"
/** 专题cell */
#import "ZJStudySpecialTableViewCell.h"
#define specialCell  @"ZJStudySpecialTableViewCell"
@interface SpecialViewController ()<UITableViewDelegate,UITableViewDataSource>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 下滑上滑控价 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation SpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewBGGrayColor];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header  beginRefreshing];
    [self setupUI];
}


- (void)setupUI{
    [self.view addSubview:self.tableView];
}
- (void)netWorking_header{
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],@"SHOW_NUMBER":@"10"};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listSpecial] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJStudySpcialModel class] json:object[@"ST_CLASS"]] mutableCopy];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
}
- (void)netWorking_footer{
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],@"SHOW_NUMBER":@"10"};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listSpecial] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZJStudySpcialModel class] json:object[@"ST_CLASS"]] ;
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}
#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudySpcialModel *model = self.dataArray[indexPath.row];
    ZJStudySpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specialCell forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudySpcialModel *model = self.dataArray[indexPath.row];
    return model.cellHeight;//专题

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudySpcialModel *model = self.dataArray[indexPath.row];
    SpecialDetailViewController *vc = [SpecialDetailViewController new];
    vc.spcialInfo = model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter


#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 44 - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (self.isStudy) {
            _tableView.height = self.view.height - 44 - LP_TopHeight - 49;
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 7.5 * [UIScreen zj_screenHScale])];
        view.backgroundColor = [UIColor viewBGGrayColor];
        _tableView.tableHeaderView = view;
        [_tableView  registerClass:[ZJStudySpecialTableViewCell class] forCellReuseIdentifier: specialCell];
      
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
